//
//  JXOrderDeliveryController.m
//  JiaoX
//
//  Created by yinqy on 15/2/3.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXOrderDeliveryController.h"
#import "OrderDeliveryItemFrame.h"
#import "OrderDeliveryItemCell.h"
#define TITLE_FONT_SIZE [DimensUtils getScaleX:16]

@interface JXOrderDeliveryController ()<UITableViewDataSource,UITableViewDelegate>
{
    OrderDelivery *_delivery;
    UITableView *_tableView;
    
    NSMutableArray *_frameArray;
}
@end

@implementation JXOrderDeliveryController

- (instancetype)initWithDelivery:(OrderDelivery *)delivery
{
    self = [super init];
    if (self) {
        _delivery = delivery;
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:JXGlobalColorBg];
    [self setTitle:@"物流信息"];
    NSString *title = [NSString stringWithFormat:@"您的订单已由%@发货\n单号:%@",_delivery.com,_delivery.nu];
    CGSize titleSize = [title sizeWithFont:[UIFont systemFontOfSize:TITLE_FONT_SIZE] constrainedToSize:CGSizeMake(JXScreenW-JXPublicInset*2, MAXFLOAT)];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(JXPublicInset, JXPublicInset+NavHeight,titleSize.width , titleSize.height)];
    [titleLabel setFont:[UIFont systemFontOfSize:TITLE_FONT_SIZE]];
    [titleLabel setTextColor:[UIColor blackColor]];
    [titleLabel setText:title];
    [titleLabel setNumberOfLines:0];
    [self.view addSubview:titleLabel];
    
    _tableView = [[UITableView alloc]init];
    [_tableView setDataSource:self];
    [_tableView setDelegate:self];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setFrame:CGRectMake(0, CGRectGetMaxY(titleLabel.frame), JXScreenW, self.view.height-CGRectGetMaxY(titleLabel.frame))];
    [self.view addSubview:_tableView];
    
    _frameArray = [NSMutableArray array];
    [self convertDataToFrame:_delivery.data];
    
}

-(void)convertDataToFrame:(NSArray *)data{
    [_frameArray removeAllObjects];
    for (DeliverItem *item in data) {
        OrderDeliveryItemFrame *frame = [[OrderDeliveryItemFrame alloc]init];
        [frame setDeliverItem:item];
        [_frameArray addObject:frame];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _frameArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderDeliveryItemCell *cell = [OrderDeliveryItemCell cellWithTableView:tableView indexpath:indexPath];
    [cell setDeliveryItemframe:_frameArray[indexPath.row]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderDeliveryItemFrame *frame = _frameArray[indexPath.row];
    return frame.frame.size.height;
}

@end
